<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">
          <img src="../../assets/logo.jpeg" width="27px" height="27px" />
          &nbsp;&nbsp;医院公众号消息通知系统
        </h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="username" v-model="loginForm.username" placeholder="请输入用户名" name="username" type="text"
          tabindex="1" auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
          placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin">
        <span v-if="loading == false">登录</span>
        <span v-else>登陆中</span>
      </el-button>


      <div class="tips">
        <div>
          <span>还没有账户？</span>
          <el-link style="line-height:100%;" type="primary">联系管理员</el-link>
        </div>
        <el-link class="right-password" type="primary">忘记密码？</el-link>
      </div>

    </el-form>
  </div>
</template>

<script>
  import key from '@/components/js/key.js'
  import {
    getInfo,
    login
  } from '@/api/user'
  export default {
    data() {
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleLogin() {
        this.loading = true;
        var that = this
        if (that.loginForm.username != false && that.loginForm.password != false) {
          var data = {
            tel: that.loginForm.username,
            password: that.loginForm.password
          }
          getInfo(data).then(res => {
            console.log('info:', res);
            var user = key.jiami(JSON.stringify(res))
            sessionStorage.setItem('admin', JSON.stringify(res))
            setTimeout(() => {
              that.$router.push({
                path: '/'
              }) // 登录成功之后重定向到首页
              that.$message({
                message: '登陆成功',
                type: 'success'
              })
            }, 1000)


          })
        } else {
          that.loading = false
          this.$message({
            message: '登录失败，请检查用户名密码',
            type: 'error'
          })
        }
        // if (this.loginForm.username == 'admin' && this.loginForm.password == '123456') {
        // } else {
        // }
      }
    }
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #283443;
  $light_gray: #0e0404;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    width: 100%;
    height: 100vh;
    overflow: hidden;
    background-color: #f8f8f8;
    background-repeat: no-repeat;
    background-position-x: center;
    background-size: 100% 100%;

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #fff;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: rgb(55, 6, 6);
  $light_gray: rgb(6, 2, 2);

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    display: grid;
    justify-items: center;
    justify-content: center;

    .login-form {
      padding: 80px 35px 80px;
      border-radius: 20px;

      position: absolute;
      width: 450px;
      height: auto;
      // margin: 0 0 0 60%;
      background: #ebe7e7;
      webkit-box-shadow: rgb(150 150 150 / 10%) 9px 8px 20px 0;
      box-shadow: 13px 10px 20px 0 rgb(150 150 150 / 10%);
    }

    .tips {
      font-size: 14px;
      color: #080b0f;
      margin-top: -15px;
      display: flex;
      justify-content: space-between;

      span {
        margin-right: 2px;
        vertical-align: middle;
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;

        >img {
          position: absolute;
          left: 20px;
          border-radius: 50%;
        }
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
